package com.cykj.service.impl;

import com.cykj.mapper.AdminMapper;
import com.cykj.pojo.Admin;
import com.cykj.pojo.Scenic;
import com.cykj.pojo.TicketsOrder;
import com.cykj.pojo.User;
import com.cykj.service.AdminService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;

import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public ResponseDto login(String name, String pwd, String cardId) {
        Admin login = adminMapper.login(name, pwd, cardId);
        if (login != null){
            //说明存在
            return new ResponseDto(1,"登陆成功",login);
        }
        return new ResponseDto(0,"该用户信息不存在",null);
    }

    @Override
    public ResponseDto register(String name, String pwd, String cardId) {
        //注册之前要先查询用户是否存在
        Admin admin = adminMapper.login(name, pwd, cardId);
        if (admin != null){
            return new ResponseDto(-1,"用户信息已经存在",null);
        }
        int register = adminMapper.register(name, pwd, cardId);
        if (register == 0){
            return new ResponseDto(0,"注册失败",null);
        }
        return new ResponseDto(1,"注册成功",register);
    }

    @Override
    public ResponseDto getUsers(int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<User> users = adminMapper.getUsers();
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto getScenic(int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<Scenic> scenicList = adminMapper.getScenic();
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto getTicketOrders(int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<TicketsOrder> ticketsOrders = adminMapper.getTicketOrders();
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto alterPwd(int id, String newPwd) {
        adminMapper.alterPwd(id, newPwd);
        return new ResponseDto(1,"修改成功",null);
    }
}
